package com.vhans.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.example.ExampleDTO;
import com.vhans.db.model.vo.example.ExampleInfoVO;
import com.vhans.db.model.vo.example.ExampleVO;
import com.vhans.db.service.IExampleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 实例Controller控制器
 * 
 * @author vhans
 * @date 2023-05-20
 */
@Api(tags = "实例模块")
@RestController
@RequestMapping("/dome/example")
public class ExampleController extends BaseController {
    @Autowired
    private IExampleService exampleService;

    /**
     * 查询实例列表
     * @param condition 条件
     * @return {@link ExampleVO} 题目列表
     */
    @ApiOperation(value = "查询实例列表")
    @SaCheckPermission("dome:example:list")
    @GetMapping("/list")
    public TableDataInfo list(ConditionDTO condition) {
        startPage();
        List<ExampleVO> list = exampleService.selectExampleVOList(condition);
        return getDataTable(list);
    }

    /**
     * 获取实例详细信息
     * @param id 实例id
     * @return {@link Result<ExampleInfoVO>} 实例信息
     */
    @ApiOperation(value = "获取实例编辑信息")
    @SaCheckPermission("dome:example:query")
    @GetMapping(value = "/info/{id}")
    public Result<ExampleInfoVO> getInfo(@PathVariable("id") Integer id) {
        return Result.success(exampleService.selectExampleInfoById(id));
    }

    /**
     * 新增实例
     *
     * @param example 实例信息
     * @return {@link Result<>}
     */
    @ApiOperation(value = "新增实例")
    @SaCheckPermission("dome:example:add")
    @PostMapping("/add")
    public Result<Integer> add(@Validated @RequestBody ExampleDTO example) {
        Integer id = exampleService.insertExample(example);
        return Result.success(id);
    }

    /**
     * 修改实例
     */
    @ApiOperation(value = "修改实例")
    @SaCheckPermission("dome:example:update")
    @PutMapping("/update")
    public Result<?> update(@Validated @RequestBody ExampleDTO example) {
        exampleService.updateExample(example);
        return Result.success();
    }

    /**
     * 删除实例
     *
     * @param idList 需要删除的实例主键列表
     * @return {@link Result<>}
     */
    @ApiOperation(value = "删除实例")
    @SaCheckPermission("dome:example:delete")
	@DeleteMapping("/delete")
    public Result<?> delete(@RequestBody List<Integer> idList) {
        return toAjax(exampleService.deleteExampleByIdIdList(idList));
    }
}
